package com.soft.zosagent.util;


import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;

import java.util.Base64;
import java.util.Random;

public class EncryptPWD {

    public static void main(String[] args) throws Exception {

        CommandLineParser parser = new GnuParser();
        Options options = new Options();

        options.addOption("enc", "encrypt", true, "Encrypt password");
        options.addOption("exptm","exptime",true,"password expired time");
        options.addOption("dec", "decrypt", true, "decrypt password");

        CommandLine commandLine = parser.parse(options, args);//解析参数

        if(!commandLine.hasOption("enc") && !commandLine.hasOption("dec")){
            System.out.println("please use parameter -enc password or -dec encpassword");
        }

        if(commandLine.hasOption("enc") && commandLine.hasOption("dec")){
            System.out.println("enc is conflict with dec.");
        }

        if(commandLine.hasOption("enc")){
            String password = commandLine.getOptionValue("enc");
            System.out.println("password:["+password+"]");

            int length = password.length();
            int mod = length%16;
            String randomString = getRandomString(16-mod);
            String lengthString=String.valueOf(length);

            if(length<10){
                lengthString = "0"+lengthString;
            }

            password = lengthString+password+randomString;

            System.out.println("encrypt password:["+encryptPwd(password)+"]");

        }else if(commandLine.hasOption("dec")) {
            String encpassword = commandLine.getOptionValue("dec");
            System.out.println("encpassword:[" + encpassword + "]");

            String decpwd = decryptPwd(encpassword);

            System.out.println("decrypt password:["+decpwd+"]");

        }else{
            System.out.println("not support parameter. please use enc or dec");
        }





    }

    public static String encryptPwd(String pwd) throws Exception
    {
        String password = pwd;
        byte[] encryptData = new byte[1024];
        int dataLength = ClassEncryptUtil.encryptData(password.getBytes(),password.getBytes().length, encryptData);

        byte[] pwdData = new byte[dataLength];
        System.arraycopy(encryptData,0,pwdData,0,dataLength);

        byte[] base64EncryptPwdData = Base64.getEncoder().encode(pwdData);
        return new String(base64EncryptPwdData);


    }

    public static String decryptPwd(String encedPassword)
    {
        byte[] encryptedPwdData = Base64.getDecoder().decode(encedPassword);
        byte[] decryptData = new byte[1024];
        int dataLength = ClassEncryptUtil.decryptData(encryptedPwdData,encryptedPwdData.length, decryptData);
        String decpwd =  new String(decryptData,0,dataLength);

        int length = Integer.parseInt(decpwd.substring(0,2));
        String password = decpwd.substring(2,length+2);
        return password;
    }

    /**
     * 获取指定长度的随机字符串
     * @param length
     * @return
     */
    public static String getRandomString(int length){
        //默认长度16
        if(length == 0){length = 16;}
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        //长度为几就循环几次
        for(int i=0; i<length; ++i){
            //产生0-61的数字
            int number=random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }

}
